package com.dream.permission

/**
 * Author: pengmutian
 * Date: 2025/3/27 11:26
 * Description: PermissionWrapper
 */

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import dev.icerock.moko.permissions.DeniedAlwaysException
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.PermissionsController
import dev.icerock.moko.permissions.compose.BindEffect
import dev.icerock.moko.permissions.compose.PermissionsControllerFactory
import dev.icerock.moko.permissions.compose.rememberPermissionsControllerFactory
import io.github.aakira.napier.Napier
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.consumeAsFlow


@Composable
fun rememberPermissionPresenter(): Pair<PermissionState, Channel<PermissionAction>> {
    val channel = remember { Channel<PermissionAction>() }
    val flow = remember(channel) { channel.consumeAsFlow()}
    val state = PermissionPresenter(action = flow)
    return state to channel
}

 @Composable
 fun PermissionPresenter(
     action: Flow<PermissionAction>,
 ): PermissionState {
     val factory: PermissionsControllerFactory = rememberPermissionsControllerFactory()
     val controller: PermissionsController = remember(factory) { factory.createPermissionsController() }
     var state by remember { mutableStateOf(dev.icerock.moko.permissions.PermissionState.NotDetermined) }
     Napier.d("PermissionPresenter state = $state")
     BindEffect(controller)
     LaunchedEffect(action) {
         action.collect {
             val permission = it.permission
             try {
                 controller.providePermission(permission)
                 state = dev.icerock.moko.permissions.PermissionState.Granted
                 Napier.d("PermissionPresenter state = $state")
                 // Permission has been granted successfully.
             } catch(deniedAlways: DeniedAlwaysException) {
                 // Permission is always denied.
                 state = dev.icerock.moko.permissions.PermissionState.DeniedAlways
                 Napier.d("PermissionPresenter state = $state")
             }
         }
     }


//     action.collectAction {
//         val permission = this.permission
//         coroutineScope.launch {
//             try {
//                 controller.providePermission(permission)
//                 state = dev.icerock.moko.permissions.PermissionState.Granted
//                 // Permission has been granted successfully.
//             } catch(deniedAlways: DeniedAlwaysException) {
//                 // Permission is always denied.
//                 state = dev.icerock.moko.permissions.PermissionState.DeniedAlways
//             } catch(denied: DeniedException) {
//                 // Permission was denied.
//                 state = dev.icerock.moko.permissions.PermissionState.Denied
//             } catch(e: Exception) {
//                 // Some other exception occurred.
//                 state = dev.icerock.moko.permissions.PermissionState.DeniedAlways
//             }
//         }
//     }
     return PermissionState(state)
 }

// 定义 PermissionAction 类
data class PermissionAction(val permission: Permission)


 data class PermissionState(
     val state: dev.icerock.moko.permissions.PermissionState,
 )